﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Enums;
using GTAuto.DAL.Extensions;

namespace GTAuto.DAL.DAL
{
    public class DALAdCategory : DALBase
    {
        public static DALAdCategory Singleton = new DALAdCategory();

        public GTAdCategory Add(GTAdCategory adCategory)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var newCategory = new AdCategory();

                newCategory.Name = adCategory.Name;
                newCategory.Active = true;
                newCategory.Description = adCategory.Description;
                newCategory.ParentId = adCategory.ParentId;

                db.AdCategories.InsertOnSubmit(newCategory);
                db.SubmitChanges();
                adCategory.Id = newCategory.Id;
                return adCategory;
            }
        }

        public GTAdCategory Update(GTAdCategory adCategory)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var category = db.AdCategories.SingleOrDefault(x => x.Id == adCategory.Id);

                category.Name = adCategory.Name;
                category.Description = adCategory.Description;
                category.Active = adCategory.Active;
                category.ParentId = adCategory.ParentId;
                db.SubmitChanges();

                return adCategory;
            }
        }


        public GTAdCategory Get(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brand = db.AdCategories.SingleOrDefault(x => x.Id == id);
                GTAdCategory gtBrand = new GTAdCategory();
                gtBrand.UpdateTo(brand);
                return gtBrand;
            }
        }

        public List<GTAdCategory> GetPaged(int page, int count, out int itemsCount)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.AdCategories
                 orderby p.Name
                 select p;


                var brands = brds.ToList();

                itemsCount = brands.Count;

                var ret = new List<GTAdCategory>();
                foreach (var brand in brands.Page(page, count))
                {
                    GTAdCategory gtBrand = new GTAdCategory();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }

        public List<GTAdCategory> GetByParentID(int parentId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.AdCategories
                 where  p.ParentId==parentId
                 orderby p.Name
                 select p;


                var brands = brds.ToList();

              

                var ret = new List<GTAdCategory>();
                foreach (var brand in brands)
                {
                    GTAdCategory gtBrand = new GTAdCategory();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }



        public List<GTAdCategory> GetMainCategories()
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.AdCategories
                 where p.ParentId == null 
                 orderby p.Name
                 select p;
                
                var brands = brds.ToList();
                
                var ret = new List<GTAdCategory>();
                foreach (var brand in brands)
                {
                    GTAdCategory gtBrand = new GTAdCategory();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }


        public void Delete(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                AdCategory singleOrDefault = db.AdCategories.SingleOrDefault(x => x.Id == id);

                if (singleOrDefault != null)
                {
                    db.AdCategories.DeleteOnSubmit(singleOrDefault);
                    db.SubmitChanges();
                }
            }
        }

    }
}
